import { BasicColumn } from '/@/components/Table';
import { FormSchema } from '/@/components/Table';
import { rules } from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
import { getWeekMonthQuarterYear } from '/@/utils';
import { values } from 'lodash-es';
import { log } from 'console';
import { t } from '/@/hooks/web/useI18n';
import { h } from 'vue';
import { Tag } from 'ant-design-vue';
//列表数据
export const columns: BasicColumn[] = [
  {
    title: '单位',
    align: 'center',
    dataIndex: 'company',
  },
  {
    title: '是否停用',
    align: 'center',
    dataIndex: 'stop',
    customRender: ({ text, record }) => {
      if (text == '1') {
        return '是';
      } else {
        return '否';
      }
    },
  },
  {
    title: '检定状态',
    dataIndex: 'statusCode',
    width: 120,
    customRender({ record }) {
      let color = '';
      let label = ''; // 新增变量用于存储状态的中文描述
      // 根据 statusCode 设置颜色和中文描述
      if (record.statusCode == '0') {
        color = '#ff4d4f';
        label = '未捡';
      } else if (record.statusCode == '1') {
        color = '#87d068';
        label = '正常';
      } else if (record.statusCode == '2') {
        color = '#fadb14';
        label = '临期';
      } else if (record.statusCode == '3') {
        color = '#fa8c16';
        label = '过期';
      }
      // 返回渲染的标签组件
      return h(Tag, { color }, () => label);
    },
  },
  {
    title: '监管所',
    align: 'center',
    dataIndex: 'regulatorsOrgCode_dictText',
  },
  {
    title: '地址',
    align: 'center',
    dataIndex: 'address',
  },
  {
    title: '计量器具名称',
    align: 'center',
    dataIndex: 'name',
  },
  {
    title: '规格参数',
    align: 'center',
    dataIndex: 'type',
  },
  {
    title: '施封状态',
    align: 'center',
    dataIndex: 'sealed_dictText',
  },
  {
    title: '出厂编号',
    align: 'center',
    dataIndex: 'code',
  },
  {
    title: 'eqcs状态',
    align: 'center',
    dataIndex: 'ecqsStatus',
  },
  {
    title: '制造单位',
    align: 'center',
    dataIndex: 'madeInCompany',
  },

  {
    title: '未处理天数（施封等活动）',
    align: 'center',
    dataIndex: 'dayNum',
    width: 200,
    customRender: ({ text, record }) => {
      // 检查 sealIds 是否存在且为数组
      if (record.sealed_dictText == '未施封') {
        let createTime = record.createTime; // 添加时间 格式为2023-11-16 10:00:00

        if (!createTime) {
          return '未知'; // 如果 createTime 不存在或无效
        }

        // 当前时间
        let currentDate = new Date(); // 动态获取当前时间

        // 将 createTime 转换为 Date 对象
        let createdDate = new Date(createTime.replace(' ', 'T')); // 替换空格确保格式兼容

        // 计算两个日期之间的时间差
        let timeDifference = currentDate.getTime() - createdDate.getTime();

        // 转换为天数（使用毫秒数进行计算）
        let dayDifference = Math.floor(timeDifference / (1000 * 3600 * 24)); // 毫秒 -> 秒 -> 分钟 -> 小时 -> 天
        return (record.dayNum = dayDifference + 1);
      } else {
        // 如果 sealIds 为空或不是数组，则返回空字符串或其他提示信息
        return '已受理';
      }
    },
  },
  {
    title: '所属区县',
    align: 'center',
    dataIndex: 'region',
  },
  {
    title: '所属区县编码',
    align: 'center',
    dataIndex: 'regionCode',
  },
  {
    title: '添加时间',
    align: 'center',
    dataIndex: 'createTime',
  },
  {
    title: '备注',
    align: 'center',
    dataIndex: 'note',
  },
];
import { useUserStore } from '/@/store/modules/user';
const userStore = useUserStore();
//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: '出厂编号',
    field: 'code',
    component: 'Input',
  },
  {
    label: '检定状态',
    field: 'statusCode',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'statusCode',
      stringToNumber: true,
    },
  },
  {
    label: '施封状态',
    field: 'sealed',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'sealed_type',
      stringToNumber: true,
    },
  },
  {
    label: '单位',
    field: 'company',
    component: 'Input',
  },
  {
    label: '区县',
    field: 'regionCode',
    component: 'JDictSelectTag',
    componentProps: {
      dictCode: 'xi_an_region_code',
      placeholder: '请选择区县',
      stringToNumber: true,
    },
    ifShow: userStore.userInfo.orgCode == 'A03',
  },
  {
    label: '未处理天数',
    field: 'dayNum',
    component: 'InputNumber',
  },
];
//表单数据
export const formSchema: FormSchema[] = [
  {
    label: '单位',
    field: 'company',
    component: 'Input',
    rules: [{ required: true, trigger: 'blur' }],
  },
  {
    label: '是否停用',
    field: 'stop',
    component: 'RadioGroup',
    componentProps: {
      //options里面由一个一个的radio组成,支持disabled
      options: [
        { label: '是', value: 1 },
        { label: '否', value: 0 },
      ],
    },
  },
  {
    label: '计量器具名称',
    field: 'name',
    component: 'Input',
    rules: [{ required: true, trigger: 'blur' }],
  },
  {
    label: '规格参数',
    field: 'type',
    component: 'Input',
    rules: [{ required: true, trigger: 'blur' }],
  },
  {
    label: '出厂编号',
    field: 'code',
    component: 'Input',
    rules: [{ required: true, trigger: 'blur' }],
  },
  {
    label: '设备号',
    field: 'deviceCode',
    component: 'Input',
    componentProps: {
      // disabled: true, // 设置为不可编
    },
  },
  {
    label: '抢号',
    field: 'gunCode',
    component: 'Input',
    componentProps: {
      // disabled: true, // 设置为不可编
    },
  },
  {
    label: '制造单位',
    field: 'madeInCompany',
    component: 'Input',
    rules: [{ required: true, trigger: 'blur' }],
  },
  {
    label: '所属区县',
    field: 'region',
    component: 'Select',
    componentProps: {
      options: [
        { value: '新城区', label: '新城区' },
        { value: '碑林区', label: '碑林区' },
        { value: '莲湖区', label: '莲湖区' },
        { value: '灞桥区', label: '灞桥区' },
        { value: '未央区', label: '未央区' },
        { value: '雁塔区', label: '雁塔区' },
        { value: '阎良区', label: '阎良区' },
        { value: '临潼区', label: '临潼区' },
        { value: '长安区', label: '长安区' },
        { value: '高陵区', label: '高陵区' },
        { value: '鄠邑区', label: '鄠邑区' },
        { value: '蓝田县', label: '蓝田县' },
        { value: '周至县', label: '周至县' },
        { value: '港务区', label: '港务区' },
        { value: '经开区', label: '经开区' },
      ],
    },
    rules: [{ required: true, trigger: 'blur' }],
  },
  {
    label: '区县编码',
    field: 'regionCode',
    component: 'Input',
    componentProps: {
      disabled: true, // 设置为不可编
    },
  },
  {
    label: '详细地址',
    field: 'address',
    component: 'Input',
    rules: [{ required: true, trigger: 'blur' }],
  },

  {
    label: '备注',
    field: 'note',
    component: 'Input',
  },
  // TODO 主键隐藏字段，目前写死为ID
  {
    label: '',
    field: 'id',
    component: 'Input',
    show: false,
  },
];

// 高级查询数据
export const superQuerySchema = {
  company: { title: '单位', order: 1, view: 'text', type: 'string' },
  address: { title: '地址', order: 2, view: 'text', type: 'string' },
  name: { title: '计量器具名称', order: 3, view: 'text', type: 'string' },
  type: { title: '规格参数', order: 4, view: 'text', type: 'string' },
  code: { title: '出厂编号', order: 5, view: 'text', type: 'string' },
  deviceCode: { title: '设备号', order: 6, view: 'text', type: 'string' },
  gunCode: { title: '抢号', order: 7, view: 'text', type: 'string' },
  madeInCompany: { title: '制造单位', order: 8, view: 'text', type: 'string' },
  region: { title: '所属区县', order: 9, view: 'text', type: 'string' },
  regionCode: { title: '所属区县编码', order: 10, view: 'text', type: 'string' },
  note: { title: '备注', order: 11, view: 'text', type: 'string' },
};

/**
 * 流程表单调用这个方法获取formSchema
 * @param param
 */
export function getBpmFormSchema(_formData): FormSchema[] {
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}
